<template>
  <el-dialog title="引用出库" v-model="dialogVisible" width="80%" style="margin-top: 40px;">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      label-width="110px"
    >
      <el-form-item label="部件名称" prop="partName">
        <el-input v-model="queryParams.partName" placeholder="请输入部件名称" @keyup.enter="getList" />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" type="primary"> 搜索</el-button>
        <el-button @click="resetQuery"> 重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="formLoading" 
      :data="list" 
      border
      ref="multipleTableRef"
      @selection-change="handleSelectionChange"
      row-key="id"
      :height="contentHeight * 0.74"
    >
      <el-table-column type="selection" width="60px" fixed="left" :reserve-selection="true" />
      <el-table-column label="序号" align="center" type="index" width="60px" fixed="left" />
      <template v-for="item, index in tableTitleList" >
        <el-table-column 
          v-if="item.isTable"
          :key="index" 
          :min-width="item.width" 
          :label="item.label" 
          :prop="item.field" 
          :align="item.align"
          :header-align="item.headerAlign"
        >
          <template #default="scope">
            <dict-tag :type="item.dictType" :value="scope.row[item.field]" v-if="item.dictType" />
            <div v-else>{{ scope.row[item.field] }}</div>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 分页 -->
    <div class="">
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import * as OutboundDeliveryApi from '@/api/outsource/outsourceInAndOutStorage/outboundDelivery'

defineProps({
  contentHeight: {
    type: Number,
    default: 0
  }
})

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const dialogVisible = ref(false)
const queryParams = ref({
  pageNo: 1,
  pageSize: 50,
  partName: undefined
})

const list = ref([])
const total = ref(0) // 列表的总页数
const multipleTableRef = ref()
const multipleSelection = ref([])
const formRef = ref()

const tableTitleList = [
  {
    isTable: true,
    width: '200px',
    label: '产品编号',
    field: 'productNumber',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '部件编号',
    field: 'partNumber',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '部件名称',
    field: 'partName',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '图纸图号',
    field: 'drawingNumber',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '部件规格',
    field: 'partSpecification',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '材质',
    field: 'material',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '规格',
    field: 'specification',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '净尺寸  (长)',
    field: 'netSizeLength',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '净尺寸  (宽)',
    field: 'netSizeWidth',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '下料尺寸(长)',
    field: 'cuttingSizeLength',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '下料尺寸(宽)',
    field: 'cuttingSizeWidth',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '库存数量',
    field: 'stockQuantity',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '库位',
    field: 'storageLocation',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '单位',
    field: 'partUnit',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  },
  {
    isTable: true,
    width: '200px',
    label: '工序名称',
    field: 'dictProcessName',
    align: 'left',
    headerAlign: 'left',
    dictType: 'craft_process_name'
  },
  {
    isTable: true,
    width: '200px',
    label: '工种',
    field: 'dictTypeOfWork',
    align: 'left',
    headerAlign: 'left',
    dictType: 'config_manufacturing_details_cutting_process'
  },
  {
    isTable: true,
    width: '200px',
    label: '下料类型',
    field: 'dictBaitingType',
    align: 'left',
    headerAlign: 'left',
    dictType: 'config_wage_baiting_type'
  },
  {
    isTable: true,
    width: '200px',
    label: '是否机加',
    field: 'dictIsMachine',
    align: 'left',
    headerAlign: 'left',
    dictType: 'is_machining'
  },
  {
    isTable: true,
    width: '200px',
    label: '机加工序',
    field: 'dictMachiningProcess',
    align: 'left',
    headerAlign: 'left',
    dictType: 'machining_process'
  },
  {
    isTable: true,
    width: '200px',
    label: '需求交期',
    field: 'requirementTime',
    align: 'left',
    headerAlign: 'left',
    dictType: ''
  }
]

const open = async (arr: []) => {
  dialogVisible.value = true
  await getList()
  nextTick(() => {
    arr.map(item => {
      list.value.map(val => {
        if (item['id'] === val['id']) {
          multipleTableRef.value.toggleRowSelection(val, true)
        }
      })
    })
  })
}
defineExpose({ open, multipleSelection }) // 提供 open 方法，用于打开弹窗

// 获取数据
const getList = async () => {
  formLoading.value = true
  try {
    const data = await OutboundDeliveryApi.getOutboundDeliveryPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    formLoading.value = false
  }
}

/** 重置按钮操作 */
const resetQuery = () => {
  formRef.value.resetFields()
  getList()
}

// 多选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const emit = defineEmits(['setDelivery'])
const submitForm = () => {
  dialogVisible.value = false
  emit('setDelivery', multipleSelection.value)
  formRef.value.resetFields()
}
</script>
